<template>
  <a-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :maskClosable="false"
    @ok="handleOk"
    @cancel="handleCancel">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item :label="$t('base.code')"  v-if="model.id" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'code', validatorRules.code]" :placeholder="$t('base.pleaseInput')"></a-input>
        </a-form-item>
        <a-form-item :label="$t('systemManagement.project')" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select v-decorator="[ 'projectCode', validatorRules.projectCode]" :placeholder="$t('base.pleaseChoose')" >
            <a-select-option v-for="item in projectList" :key="item.id" :value="item.code">
              {{item.name}}
            </a-select-option>
          </a-select>
        </a-form-item>

        <a-form-item :label="$t('light.lightPoleType')" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag  v-decorator="['lightPoleType', validatorRules.lightPoleType]" :triggerChange="true" :placeholder="$t('base.pleaseSelect')"
                              dictCode="glr_light_pole_type"/>
        </a-form-item>

        <a-form-item v-if="!this.model.id" :label="$t('base.num')" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number style="width: 100%" :min="1" :max="999" :placeholder="$t('base.pleaseInput')" v-decorator="[ 'poleNum', validatorRules.poleNum]" />
        </a-form-item>

        <a-form-item :label="$t('base.description')" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-textarea :placeholder="$t('base.pleaseInput')" v-decorator="['memo', {}]"  />
        </a-form-item>

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JDate from '@/components/jeecg/JDate'

  export default {
    name: "GlrLightPoleModal",
    components: {
      JDate,
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title:this.$t('base.operation'),
        width:800,
        visible: false,
        typeList:[],
        lightPoleList:[],
        projectList:[],
        disableMixinCreated:true,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          code: {rules: [
          ]},
          name: {rules: [
            ]},
          lightPoleType: {rules: [
              {required: true, message: this.$t('base.pleaseSelect')},
            ]},
          projectCode: {rules: [
              {required: true, message: this.$t('base.pleaseSelect')},
            ]},
          synchronizationGateWay: {rules: [
          ]},
          longitude: {rules: [
            ]},
          poleNum: {rules: [
              {required: true, message: this.$t('base.pleaseInput')},
            ]},
          latitude: {rules: [
          ]},
          memo: {rules: [
          ]},
          sysCompanyCode: {rules: [
          ]},
        },
        url: {
          add: "/glr/glrLightPole/add",
          edit: "/glr/glrLightPole/edit",
          getLightPoleList: "/glr/glrLightPole/getLightPoleList",
          getProjectList: "/glr/glrProject/getProject",
        }
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        const that = this;
        httpAction(that.url.getProjectList,{},"post").then((res)=>{
          if(res.success){
            that.projectList=res.result;
          }else{
            that.$message.warning(res.message);
          }
        }).finally(() => {
          that.confirmLoading = false;
        })
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        if(!this.model.id){
          this.model.poleNum=1;
          this.model.lightPoleType="1";
        }
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'poleNum','memo','lightPoleCode','code','lightPoleType','longitude','latitude','projectCode','sysCompanyCode'))
        })
        if (this.model.projectCode){
          this.handleChangeProjectCode(this.model.projectCode);
        }
        this.form.setFieldsValue({'lightPoleType': '1'});


      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);

            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }

        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'code','name','lightPoleType','memo','synchronizationGateWay','longitude','latitude','projectCode','sysCompanyCode'))
      },
      handleChangeProjectCode(projectCode){
        const that = this;
        httpAction(that.url.getLightPoleList,{projectCode:projectCode},"post").then((res)=>{
          if(res.success){
            that.lightPoleList=res.result;
            if (that.lightPoleList.length>0){
              that.form.setFieldsValue({'lightPoleCode': that.lightPoleList[0].code});
            }
          }else{
            that.$message.warning(res.message);
          }
        }).finally(() => {
          that.confirmLoading = false;
        })
      }


    }
  }
</script>